<?php

namespace App\Department\Controllers;

use App\Extensions\Actions\Import\UserImport;
use App\Models\AdminUser;
use App\Services\AdminUserService;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use App\Repositories\AdminUserRepository;
use Encore\Admin\Controllers\HasResourceActions;

class UserController extends AdminController
{
    use HasResourceActions;

    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '用户管理';
    public $repository;
    public $service;


    public function __construct(AdminUserRepository $repository, AdminUserService $service)
    {
        $this->repository = $repository;
        $this->service = $service;
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = $this->repository->deptGrid();
        $grid->tools(function (Grid\Tools $tools) {
            $tools->append(new UserImport());
        });
        $grid = $this->service->getGridColumn($grid);
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
            //$actions->disableDelete();
        });
        return $grid;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return $this->service->form();
    }

    public function destroy($id)
    {
        $model = AdminUser::query()->findOrFail($id);
        if ($model->role_type || $model->department_id != Admin::user()->department_id) {
            return response()->json([
                'status' => false,
                'message' => '没有权限',
            ]);
        }
        return $this->form()->destroy($id);
    }
}
